package cn.edu.gsau.info.service.impl;


import cn.edu.gsau.info.dao.RongyuchenghaoDao;
import cn.edu.gsau.info.models.Rongyuchenghao;
import cn.edu.gsau.info.models.User;
import cn.edu.gsau.info.models.Zhengmingcailiao;
import cn.edu.gsau.info.myenum.ModuleIdClass;
import cn.edu.gsau.info.service.IRongyuchenghaoService;
import cn.edu.gsau.info.service.IZhengmingcailiaoService;
import cn.edu.gsau.info.vo.FileListName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jinjiang
 * @since 2022-11-17
 */
@Service
public class RongyuchenghaoServiceImpl extends ServiceImpl<RongyuchenghaoDao, Rongyuchenghao> implements IRongyuchenghaoService {
    @Autowired
    RongyuchenghaoDao rongyuchenghaoDao;
    //分页查询
    @Override
    public IPage selectById(long id) {
        IPage<Rongyuchenghao> page = new Page<>(1, 5);
        QueryWrapper qw = new QueryWrapper();
        qw.eq("tianbaoId", id);
        IPage iPage = rongyuchenghaoDao.selectPage(page, qw);
        return iPage;
    }
    //荣誉称号id查询
    @Override
    public Rongyuchenghao selectByRongyuchenghaoId(int id) {
        Rongyuchenghao rongyuchenghao = rongyuchenghaoDao.selectByRongyuchenghaoId(id);
        return rongyuchenghao;
    }
    //关键字查询
    @Override
    public List<Rongyuchenghao> selectByStr(String string) {
        List<Rongyuchenghao> selectByStr = rongyuchenghaoDao.selectByStr(string);
        return selectByStr;
    }
    //状态查询
    @Override
    public List<Rongyuchenghao> selectByStatus(String stauts) {
        List<Rongyuchenghao> selectByStatus = rongyuchenghaoDao.selectByStatus(stauts);
        return selectByStatus;
    }
    //关键字和状态查询
    @Override
    public List<Rongyuchenghao> selectLike(String string, String status) {
        List<Rongyuchenghao> selectLike = rongyuchenghaoDao.selectLike(string, status);
        return selectLike;
    }
    //添加数据
    @Autowired
    IZhengmingcailiaoService zhengmingcailiaoDao;
    @Override
    public void add(Rongyuchenghao rongyuchenghao) {
        User user = new User();
        user.setId(6L);
        user.setUserName("ylz");
        rongyuchenghao.setTianbaoId(user.getId());
        rongyuchenghao.setStatus("进行中");
        rongyuchenghao.setCreateTime(new Date());
        rongyuchenghao.setNode("审核");
        rongyuchenghaoDao.insert(rongyuchenghao);
        Long id = rongyuchenghao.getId();
        FileListName[] fileListNames = rongyuchenghao.getFileList();
        for (int i = 0; i < fileListNames.length; i++) {
            Zhengmingcailiao zhengmingcailiao = new Zhengmingcailiao();
            zhengmingcailiao.setModuleId(ModuleIdClass.RONGYUCHENGHAO_id);
            zhengmingcailiao.setRecoreId(id);
            zhengmingcailiao.setFileName(fileListNames[i].getOldfilename());
            File path = new File(user.getUserName());
            if(path.exists()==false) {
                path.mkdirs();
            }
            zhengmingcailiao.setFileFullPath(path +"/" + fileListNames[i].getNewfilename());
            zhengmingcailiaoDao.save(zhengmingcailiao);
        }
    }
}